﻿// CosignConfiguration.cs
// 
// Copyright (C) 2011 The Pennslyvania State University
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty 
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
// PURPOSE. See the GNU General Public License for more details. 
// You should have received a copy of the GNU General Public 
// License along with this program; if not, write to the Free 
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
// MA 02111-1307 USA

#region

using System;
using Microsoft.Web.Administration;

#endregion

namespace CosignManaged
{
    internal class CosignConfiguration
    {
        public String ServiceName { get; private set; }
        public String CertCommonName { get; private set; }
        public String CosignServerName { get; private set; }
        public String CosignServerUrl { get; private set; }
        public int CosignServerPort { get; private set; }
        public String CookieDb { get; private set; }
        public int CookieTimeOut { get; private set; }
        public int Protected { get; private set; }
        public String CosignErrorUrl { get; private set; }
        public String SiteEntryUrl { get; private set; }
        public String ValidReference { get; private set; }
        public Boolean SecureCookies { get; private set; }
        public Boolean HttpOnlyCookies { get; private set; }
        public String KerbDbPath { get; private set; }
        public int ServerRetries { get; private set; }
        public Boolean FallBackBasicAuth { get; private set; }
        public String BasicAuthRealm { get; private set; }
        public String BasicAuthDomain { get; private set; }

        /// <summary>
        ///   Load the cosign configuration from the configuration files using the proper hierarchy.
        /// </summary>
        public CosignConfiguration()
        {
            try
            {
                ConfigurationSection config = WebConfigurationManager.GetSection("system.webServer/cosign");
                ServiceName = (String) config.GetChildElement("service").GetAttributeValue("name");
                if (String.IsNullOrEmpty(ServiceName))
                    throw new Exception("Config file missing service name");
                CertCommonName = (String) config.GetChildElement("crypto").GetAttributeValue("certificateCommonName");
                if (String.IsNullOrEmpty(CertCommonName))
                    throw new Exception("Config file missing cert common name");
                CosignServerName = (String) config.GetChildElement("webloginServer").GetAttributeValue("name");
                if (String.IsNullOrEmpty(CosignServerName))
                    throw new Exception("Config file missing cosign server name");
                CosignServerUrl = (String) config.GetChildElement("webloginServer").GetAttributeValue("loginUrl");
                if (String.IsNullOrEmpty(CosignServerUrl))
                    throw new Exception("Config file missing cosign server url");
                CosignServerPort = (int) config.GetChildElement("webloginServer").GetAttributeValue("port");
                if (CosignServerPort == 0)
                    throw new Exception("Config file missing cosign server port");
                CookieDb = (String) config.GetChildElement("cookieDb").GetAttributeValue("directory");
                if (String.IsNullOrEmpty(CookieDb))
                    throw new Exception("Config file missing Cookie db directory");
                Protected = (int) config.GetChildElement("protected").GetAttributeValue("status");
                CosignErrorUrl = (String) config.GetChildElement("validation").GetAttributeValue("errorRedirectUrl");
                if (String.IsNullOrEmpty(CosignErrorUrl))
                    throw new Exception("Config file missing cosign error url");
                SiteEntryUrl = (String) config.GetChildElement("siteEntry").GetAttributeValue("url");
                ValidReference = (String) config.GetChildElement("validation").GetAttributeValue("validReference");
                if (String.IsNullOrEmpty(ValidReference))
                    throw new Exception("Config file missing validation reference");
                CookieTimeOut = (int) config.GetChildElement("cookieDb").GetAttributeValue("expireTime");
                if (CookieTimeOut == 0)
                    throw new Exception("Config file missing cookie timeout");
                SecureCookies = (Boolean) config.GetChildElement("cookies").GetAttributeValue("secure");
                HttpOnlyCookies = (Boolean) config.GetChildElement("cookies").GetAttributeValue("httpOnly");
                KerbDbPath = (String) config.GetChildElement("kerberosTickets").GetAttributeValue("directory");
                ServerRetries = (int) config.GetChildElement("webloginServer").GetAttributeValue("connectionRetries");
                if (ServerRetries == 0)
                    throw new Exception("Config file missing server retries");
                FallBackBasicAuth = (Boolean) config.GetChildElement("basicAuth").GetAttributeValue("fallback");
                BasicAuthRealm = (String) config.GetChildElement("basicAuth").GetAttributeValue("realm");
                BasicAuthDomain = (String) config.GetChildElement("basicAuth").GetAttributeValue("domain");
            }
            catch (Exception ex)
            {
                throw new Exception("Configuration is invalid", ex);
            }
        }
    }
}